<?php namespace Home\Controller; 

use Common\Model\Gorder;

//购物车提交控制器   
class GorderController extends CommonController{

	private $db;
	//构造函数
	public function __init()
	{
		parent::__init();
		
		$this->db=new Gorder;
		
	}
	
    //动作
    public function index(){
		if(IS_POST){
//			var_dump($_POST);die;
			if($_POST['ads']!='on'){
				$uaid=$_POST['ads'];
				$addressModel=new \Common\Model\User_address;
				$addressData=$addressModel->where('uaid',$uaid)->find();
//				var_dump($addressData);die;
				$_POST=array(
					'consignee'=>$addressData['consignee'],
					'address'=>$addressData['address'],
					'phone'=>$addressData['phone'],
					'postcode'=>$addressData['code']				
				);
				$result=$this->db->store();
				
			}else{
				$address=Q('post.address');
				$_POST['address']=implode(',', $address);
	//			var_dump($_POST);die;
				$result=$this->db->store();
				
			}
						
			if(!$result){				
				View::error($this->db->getError());
//				调用Model中的getError()方法  里边储存了$validate返回的错误信息
			}
			$oid=$result;
			
			View::success('生成订单成功!',U('index',array('oid'=>$oid)));
		};
		
//		p($_SESSION);die;
		$oid=Q('get.oid',0,'intval');  
//		var_dump($oid);die;
		$orderData=$this->db->where('oid',$oid)->find();	   
//		var_dump($orderData);die;   
		View::with('orderData',$orderData);  
		$userModel=new \Common\Model\User;
		$userData=$userModel->where('uid',$orderData['user_uid'])->find();	   
//		var_dump($userData);die;   
		View::with('userData',$userData);  
	   View::make();
    }
	
	public function goMember(){
//		p($_SESSION);die;
    	if(IS_POST){
//  		提交支付
//			p($_POST);die;
			$onumber=Q('post.onumber');
//			获取到订单号，将当前的订单更改为待发货
//			支付成功后将订单状态更改为待发货
			$data=Db::table('gorder')->where('onumber',$onumber)->update(array('status'=>'待发货'));
//			支付成功后跳转到会员中心,查看用户的所有订单详细
//			go(U('Member/orderList'));
			unset($_SESSION['cart']);
			View::success('付款成功',U('Usercenter/orderList'));

    	}
		
		
		
		
		
		
	}
	
	
	
}